package com.zhenyi.jiahua.interceptor;

import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhRoleResource;
import com.jiahua.common.model.JhStaff;

import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

public class HasPermissionFreeMarkerMethod implements TemplateMethodModelEx {

	private JhStaff staff;

	public HasPermissionFreeMarkerMethod(JhStaff staff) {
		this.staff = staff;
	}

	@Override
	public Object exec(@SuppressWarnings("rawtypes") List list) throws TemplateModelException {
		if (null == list || 1 != list.size()) {
			throw new TemplateModelException("Wrong arguments: only one argument is allowed");
		}
		String permissionName = String.valueOf(list.get(0));
		Kv kv = Kv.by("name", permissionName);
		kv.set("staffId", staff.getId());
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindStaffResource", kv);
		JhRoleResource roleResource = JhRoleResource.dao.findFirst(sqlPara);
		if (roleResource == null) {
			return false;
		}
		Integer YES = 1;
		if (YES.equals(roleResource.getCanShow())) {
			return true;
		}
		return false;
	}
}
